﻿Imports Entities
Public Class ClienteDal
    Public Shared Function Create(ByVal zObject As CLIENTES) As CLIENTES
        Try
            Using bd As New gas_gasdesuvecinoEntities
                bd.CLIENTES.Add(zObject)
                bd.SaveChanges()
                Return zObject
            End Using
        Catch ex As Exception
            Debug.Print("ClienteDal.Create: " + ex.Message)
            Return Nothing
        End Try
    End Function
    Public Shared Function List() As List(Of CLIENTES)
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = From rec In bd.CLIENTES
                          Where rec.ACTIVO
                            Order By rec.NOMBRE Ascending
                          Select rec

                Return query.ToList
            End Using
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
   
    Public Shared Function ListByCalleId(ByVal zid As Long) As List(Of CLIENTES)
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = From rec In bd.CLIENTES
                            Where rec.CALLE = zid And rec.ACTIVO
                              Order By rec.NOMBRE Ascending
                            Select rec
              
                Return query.ToList
            End Using
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Shared Function Existe(ByVal zNumero As String, ByVal zCalle As Long) As Boolean
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.CLIENTES
                            Where rec.CALLE = zCalle And rec.NUMERO = zNumero
                            Select rec).Count
                If query > 0 Then
                    Return True
                Else
                    Return False
                End If
            End Using
        Catch ex As Exception
            Return False
        End Try
    End Function
    Public Shared Function Existe(ByVal zId As Long) As Boolean
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.CLIENTES
                            Where rec.CLIENTE = zId
                            Select rec).Count
                If query > 0 Then
                    Return True
                Else
                    Return False
                End If
            End Using
        Catch ex As Exception
            Return False
        End Try
    End Function
    Public Shared Function SearchByDireccion(ByVal zNumero As String, ByVal zCalle As Long) As CLIENTES
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = From rec In bd.CLIENTES
                          Where rec.NUMERO = zNumero And rec.CALLE = zCalle And rec.ACTIVO
                          Select rec

                Return query.SingleOrDefault
            End Using
        Catch ex As Exception
            Debug.Print("ClienteDAL " + ex.Message)
            Return Nothing
        End Try
    End Function
    Public Shared Function SearchById(ByVal zId As Long) As CLIENTES
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = From rec In bd.CLIENTES
                          Where rec.CLIENTE = zId
                          Select rec

                Return query.Single
            End Using
        Catch ex As Exception
            Debug.Print("ClienteDAL " + ex.Message)
            Return Nothing
        End Try
    End Function
    Public Shared Function SearchByIdALL(ByVal zId As Long) As CLIENTES
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = From rec In bd.CLIENTES
                          Where rec.CLIENTE = zId
                          Select rec

                Return query.Single
            End Using
        Catch ex As Exception
            Debug.Print("ClienteDAL " + ex.Message)
            Return Nothing
        End Try
    End Function
  
    Public Shared Function Update(ByVal zObject As CLIENTES) As CLIENTES
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.CLIENTES
                               Where rec.CLIENTE = zObject.CLIENTE
                              Select rec).Single

                query.NOMBRE = zObject.NOMBRE
                query.FONO = zObject.FONO
                query.RUT = zObject.RUT
                query.CALLE = zObject.CALLE

                query.FECHADEVOLUCION = zObject.FECHADEVOLUCION
                query.ACTIVO = True
                bd.SaveChanges()
                Return query
            End Using
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Public Shared Function Delete(ByVal zId As Long) As Boolean
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.CLIENTES
                          Where rec.CLIENTE = zId).SingleOrDefault

                query.ACTIVO = False
                bd.SaveChanges()
                Return True
            End Using
        Catch ex As Exception
            Return False
        End Try
    End Function
End Class
